/**
 * 获取元素高度，这个方法局限性非常大，等待后续的解决方案
 * fixme
 * @param {HTMLElement} el html元素
 */
const actualHeight = function (el) {
  let el_style = window.getComputedStyle(el)

  let children = el.children || [],
    childrenHeight = 0
  for (let i = 0; i < children.length; i++) {
    let childStyle = window.getComputedStyle(children[i])
    if (childStyle.display !== 'none' && childStyle.position === "static") {
      childrenHeight += parseInt(childStyle.height)
    }
  }
  childrenHeight += el_style.paddingBottom.replace('px', '') - 0
  childrenHeight += el_style.paddingTop.replace('px', '') - 0

  return childrenHeight
}
/**
 * 不定高度元素transition动画
 * @param {DOMElement} element 元素
 * @param {string} time 动画时间
 */
const transitionHeight = function (element, time = 200) {
  if (typeof window.getComputedStyle == "undefined") return;

  let height = window.getComputedStyle(element).height;

  element.style.transition = "none";

  element.style.height = "auto";
  const targetHeight = window.getComputedStyle(element).height;
  element.style.height = height;
  if (time) element.style.transition = "height " + time + "ms";
  element.style.height = targetHeight;
}

const getHeight = function (el) {
  const el_style = window.getComputedStyle(el),
    el_display = el_style.display,
    el_position = el_style.position,
    el_visibility = el_style.visibility,
    el_max_height = el_style.maxHeight.replace('px', '').replace('%', ''),
    wanted_height = 0;
  // if its not hidden we just return normal height
  if (el_display !== 'none' && el_max_height !== '0') {
    return el.offsetHeight;
  }
  // the element is hidden so:
  // making the el block so we can meassure its height but still be hidden
  el.style.position = 'absolute';
  el.style.visibility = 'hidden';
  el.style.display = 'block';
  wanted_height = el.offsetHeight;
  // reverting to the original values
  el.style.display = el_display;
  el.style.position = el_position;
  el.style.visibility = el_visibility;
  return wanted_height;
}

export {
  actualHeight,
  transitionHeight,
  getHeight
}